using UnityEngine;

public class SFX_BubbleSize : MonoBehaviour
{
    public AudioClip[] randClips;
    public AudioSource audioSource;
    public float pitchMin = 0.7f;
    public float pitchMax = 1.3f;
    public float minTime = 0.15f;
    float timePassed;
    bool isPlaying;

    public void ResetPlayer()
    {
        audioSource.Stop();
        audioSource.pitch = pitchMin;
        isPlaying = false;
    }

    public void Play()
    {
        isPlaying = true;
    }

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Awake()
    {
        audioSource.loop = false;
        ResetPlayer();
    }

    // Update is called once per frame
    void Update()
    {
        if (isPlaying && !audioSource.isPlaying && timePassed > minTime) {
            var i = Random.Range(0, randClips.Length);
            audioSource.clip = randClips[i];
            audioSource.pitch = Mathf.Lerp(pitchMin, pitchMax, Bubble.I.size_p);
            audioSource.Play();
            timePassed = 0;
        }
        timePassed += Time.deltaTime;
    }
}
